package com.hcms.admin.controller.sys;

import cn.hutool.core.util.StrUtil;
import com.hcms.admin.annotation.CheckPermission;
import com.hcms.admin.dto.SysMenuTreeResponse;
import com.hcms.admin.dto.SysRoleOptionResponse;
import com.hcms.admin.service.ISysMenuService;
import com.hcms.admin.service.ISysRolePermissionService;
import com.hcms.admin.service.ISysRoleService;
import com.hcms.common.api.CommonPage;
import com.hcms.common.api.CommonResponse;
import com.hcms.mybatis.entity.SysRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 后台用户角色表 前端控制器
 * </p>
 *
 * @author achenxw@126.com
 * @since 2023-02-13
 */
@Api
@RestController
@RequestMapping("/sysRole")
public class SysRoleController {

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysMenuService sysMenuService;

    @Autowired
    private ISysRolePermissionService sysRolePermissionRelationService;

    @ApiOperation("获取角色列表")
    @GetMapping("")
    @ResponseBody
    public ResponseEntity<CommonResponse<CommonPage<SysRole>>> index(@RequestParam(required = false) String keyword, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        List<SysRole> list = sysRoleService.getList(keyword, pageNum, pageSize);
        return CommonResponse.ok(CommonPage.restPage(list));
    }

    @ApiOperation("获取角色详情")
    @GetMapping("/{id}")
    @ResponseBody
    public ResponseEntity<CommonResponse<SysRole>> view(@PathVariable Long id) {
        SysRole role = sysRoleService.getView(id);
        if (role == null) {
            return CommonResponse.notFound("记录不存在");
        }
        return CommonResponse.ok(role);
    }

    @ApiOperation("添加角色")
    @PostMapping("")
    @ResponseBody
    public ResponseEntity<CommonResponse<SysRole>> create(@RequestBody SysRole role) {
        int count = sysRoleService.createItem(role);
        if (count > 0) {
            return CommonResponse.created(role);
        }
        return CommonResponse.badRequest("添加失败");
    }

    @ApiOperation("更新角色")
    @PutMapping("/{id}")
    @ResponseBody
    public ResponseEntity<CommonResponse<Object>> update(@PathVariable Long id, @RequestBody SysRole role) {
        int count = sysRoleService.updateItem(id, role);
        if (count > 0) {
            return CommonResponse.accepted("更新成功");
        }
        return CommonResponse.badRequest("更新失败");
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/{ids}")
    @ResponseBody
    public ResponseEntity<CommonResponse<Object>> delete(@PathVariable String ids) {
        int count = sysRoleService.deleteItem(ids);
        if (count > 0) {
            return CommonResponse.noContent("删除成功");
        }
        return CommonResponse.badRequest("删除失败");
    }

    @ApiOperation("获取角色权限")
    @GetMapping("/rolePerm")
    public ResponseEntity<CommonResponse<HashMap<String, Object>>> rolePerm(@RequestParam Long roleId) {
        List<SysMenuTreeResponse> treeData = sysMenuService.getTreeList();
        List<String> rolePermissions = sysRolePermissionRelationService.getPermissionsByRoleId(roleId);
        HashMap<String, Object> result = new HashMap<>();
        result.put("treeData", treeData);
        result.put("permissions", rolePermissions);
        return CommonResponse.ok(result);
    }

    @ApiOperation("设置角色权限")
    @PostMapping("/rolePerm")
    public ResponseEntity<CommonResponse<Object>> rolePerm(@RequestParam Long roleId, String permissions) {
        String[] split = StrUtil.isEmpty(permissions) ? new String[0] : permissions.split(",");
        List<String> list = Arrays.asList(split);
        sysRoleService.setRolePermissions(roleId, list);
        return CommonResponse.accepted("修改成功");
    }

    @ApiOperation("获取角色选项")
    @GetMapping("/options")
    @ResponseBody
    @CheckPermission(check = false)
    public ResponseEntity<CommonResponse<List<SysRoleOptionResponse>>> options() {
        List<SysRole> list = sysRoleService.list();
        List<SysRoleOptionResponse> collect = list.stream().map(SysRoleOptionResponse::new).collect(Collectors.toList());
        return CommonResponse.ok(collect);
    }
}
